package com.asj.service.command.specific;

import static com.asj.enums.AppIdentifierDictEnums.is_lasted_op;
import static com.asj.enums.AppIdentifierDictEnums.supervision_NotSave;
import static com.asj.enums.AppIdentifierDictEnums.supervision_cancel;
import static com.asj.enums.ControlProgressBarEnum.cancelSupervisionSuccess;
import static com.asj.enums.ControlProgressBarEnum.clearBlackList;
import static com.asj.enums.DeviceIdentifierDictEnums.cancelSupervision;
import static com.asj.enums.DeviceIdentifierDictEnums.json_key_resultMsg;
import static com.asj.enums.HttpRequestDictEnums.receiveCancelSupervisionResultUrl;
import static com.asj.enums.ResultCodeEnums.request_error;
import static com.asj.enums.ResultCodeEnums.result_error;
import static com.asj.enums.ResultCodeEnums.result_success;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.asj.MainActivity;
import com.asj.MainActivityService;
import com.asj.enums.ControlProgressBarEnum;
import com.asj.service.StayAliveService;
import com.asj.service.oppoSdk.OPPOControlService;
import com.asj.utils.CommonsUtils;
import com.asj.utils.HttpRequestUtils;
import com.asj.utils.JSONParamPackingUtils;
import com.google.gson.JsonObject;

/**
 * 取消监管
 * */
public class SpecificCancelSupervisionService {
    public static void cancelSupervision(JsonObject contentData, Context context){
        String title = "监管已取消";
        String contentMsg = "设备已移除监管状态！恢复正常情况！";
        try{
            OPPOControlService oppoControlService = new OPPOControlService(context);
            //取消监管
            oppoControlService.initiateOrCancelSupervision(supervision_NotSave.getValue(),supervision_cancel.getValue());
            //清空所有黑名单
            oppoControlService.clearBlackList();
            //销毁常驻后台任务
            MainActivityService.stopMeService(context);

            JSONParamPackingUtils.functionPacking(contentData,result_success, null);
        }catch (Exception e){
            JSONParamPackingUtils.functionPacking(contentData,result_error, e.getMessage());
            title = "监管取消异常！";
            contentMsg = e.getMessage();
        }
        //发送通知提示取消监管
        assert contentMsg != null;
        CommonsUtils.showNotification(context,title,contentMsg);
        new Thread(()->{
            //将请求发送回去
            String result = HttpRequestUtils.retryHttpRequest(receiveCancelSupervisionResultUrl.getValue(), contentData.toString(), result_success.getCode(), 5,context);
            if(!result.equals(request_error.getMessage())){
                Log.i("取消监管已返回结果，告知成功！",result);
            }
        }).start();
    }
}
